package liziy.spring.core.classreading.type;

import liziy.spring.beans.factory.annotation.Scope;
import liziy.spring.beans.factory.config.BeanDefinition;
import liziy.spring.context.annotation.AnnotatedBeanDefintion;

/**
 * @author Liziy
 * @date 2020/12/17 20:13
 **/
public class ScopeMetadataResolver implements ScopeMetadata {

    private AnnotatedBeanDefintion beanDefintion;
    public ScopeMetadataResolver(AnnotatedBeanDefintion definition) {
        this.beanDefintion = definition;
    }

    /**
     * 解析作用域名称
     * @return
     */
    @Override
    public String resolverScopeName() {
        String scopeName = BeanDefinition.SCOPE_PROTOTYPE;
        //判断是否有@Scope注解
        Class<?> beanClass = beanDefintion.getBeanClass();
        if (beanClass.isAnnotationPresent(Scope.class)) {
            Scope annotation = beanClass.getAnnotation(Scope.class);
            if (!"".equals(annotation.value())) {
                //获取用户自定义作用域名称
                scopeName = annotation.value();
            }
        }
        return scopeName;
    }
}
